package com.wing.product.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.UniqueIdGeneratorUtil;
import com.wing.product.dao.WarehouseComputerPracticeDao;
import com.wing.product.dao.WarehouseProcessingReportDao;
import com.wing.product.dao.WarehouseProcessingReportDetailDao;
import com.wing.product.model.entity.*;
import com.wing.product.model.request.WarehouseProcessingReportDetailForm;
import com.wing.product.model.request.WarehouseProcessingReportForm;
import com.wing.product.model.response.WarehouseProcessingReportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class WarehouseProcessingReportServiceImpl extends ServiceImpl<WarehouseProcessingReportDao, WarehouseProcessingReport> implements WarehouseProcessingReportService{


    @Autowired
    WarehouseProcessingReportDetailService warehouseProcessingReportDetailService;
    @Autowired
    WarehouseProcessingReportDetailDao warehouseProcessingReportDetailDao;
    @Autowired
    WarehouseProductInventoryService warehouseProductInventoryService;
    @Autowired
    WarehouseComputerPracticeService warehouseComputerPracticeService;
    @Autowired
    WarehouseComputerPracticeDao warehouseComputerPracticeDao;
    @Autowired
    WarehouseProductionOrderService warehouseProductionOrderService;
    @Autowired
    WarehouseProductionSchedulingPlanService warehouseProductionSchedulingPlanService;
    @Autowired
    WarehouseProductionSchedulingPlanDetailService warehouseProductionSchedulingPlanDetailService;

    @Override
    public Page<WarehouseProcessingReportVO> pageList4app(Page page, Map<String, Object> params) {
        List<WarehouseProcessingReportVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public WarehouseProcessingReportVO selectProcessingReport(String id){

        WarehouseProcessingReportVO warehouseProcessingReportVO = this.baseMapper.selectProcessingReport(id);
        LambdaQueryWrapper<WarehouseProcessingReportDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WarehouseProcessingReportDetail::getWarehouseProcessingReportId,warehouseProcessingReportVO.getId())
                .eq(WarehouseProcessingReportDetail::getIsDeleted,0);
        List<WarehouseProcessingReportDetail> warehouseProcessingReportDetails = warehouseProcessingReportDetailDao.selectList(lambdaQueryWrapper);
        warehouseProcessingReportVO.setWarehouseProcessingReportDetailList(warehouseProcessingReportDetails);
        return  warehouseProcessingReportVO;

    }

    @Override
    @Transactional
    public JsonResult saveWarehouseProcessingReport(WarehouseProcessingReportForm warehouseProcessingReportForm){

        if (ObjectUtil.isNotEmpty(warehouseProcessingReportForm)){
            //添加加工记录
            WarehouseProcessingReport warehouseProcessingReport = new WarehouseProcessingReport();
            CopyOptions copyOptions = CopyOptions.create()
                    .setEditable(WarehouseProcessingReport.class)
                    .setIgnoreError(true)
                    .setIgnoreNullValue(true);
            BeanUtil.copyProperties(warehouseProcessingReportForm, warehouseProcessingReport, copyOptions);
            warehouseProcessingReport.setCode(UniqueIdGeneratorUtil.generateUniqueId("JG", "yyyyMMddHHmmss", 1000, 9999));
            warehouseProcessingReport.setStatus("1");

            //上机记录
            LambdaQueryWrapper<WarehouseComputerPractice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(WarehouseComputerPractice::getId,warehouseProcessingReportForm.getComputerPracticeId());
            WarehouseComputerPractice warehouseComputerPractice = warehouseComputerPracticeDao.selectOne(lambdaQueryWrapper);

            //查询生产订单
            LambdaQueryWrapper<WarehouseProductionOrder> lambdaQueryWrapperProductionOrder = new LambdaQueryWrapper<>();
            lambdaQueryWrapperProductionOrder.eq(WarehouseProductionOrder::getId,warehouseProcessingReportForm.getProductionOrderId());
            WarehouseProductionOrder warehouseProductionOrder = warehouseProductionOrderService.getOne(lambdaQueryWrapperProductionOrder);

            //查询生产排产计划明细
            LambdaQueryWrapper<WarehouseProductionSchedulingPlanDetail> lambdaQueryWrapperProductionSchedulingPlanDetail = new LambdaQueryWrapper<>();
            lambdaQueryWrapperProductionSchedulingPlanDetail.eq(WarehouseProductionSchedulingPlanDetail::getId,warehouseComputerPractice.getProductionSchedulingPlanDetailId());
            WarehouseProductionSchedulingPlanDetail warehouseProductionSchedulingPlanDetail = warehouseProductionSchedulingPlanDetailService.getOne(lambdaQueryWrapperProductionSchedulingPlanDetail);

            //查询生产排产计划
            LambdaQueryWrapper<WarehouseProductionSchedulingPlan> lambdaQueryWrapperProductionSchedulingPlan = new LambdaQueryWrapper<>();
            lambdaQueryWrapperProductionSchedulingPlan.eq(WarehouseProductionSchedulingPlan::getId,warehouseProductionSchedulingPlanDetail.getWarehouseProductionSchedulingPlanId());
            WarehouseProductionSchedulingPlan warehouseProductionSchedulingPlan = warehouseProductionSchedulingPlanService.getOne(lambdaQueryWrapperProductionSchedulingPlan);

            warehouseProcessingReport.setProductionPlanCode(warehouseProductionSchedulingPlan.getProductionPlanCode());
            warehouseProcessingReport.setProductionPlanDetailCode(warehouseComputerPractice.getProductionSchedulingPlanDetailCode());
            this.baseMapper.insert(warehouseProcessingReport);

            Integer prodictionNumber = 0;
            //添加加工记录明细
            if (CollectionUtils.isNotEmpty(warehouseProcessingReportForm.getWarehouseProcessingReportDetailList())){
                List<WarehouseProductInventory> warehouseProductInventoryList = new ArrayList<>();
                List<WarehouseProcessingReportDetail> warehouseProcessingReportDetailList = new ArrayList<>();
                for (WarehouseProcessingReportDetailForm pr : warehouseProcessingReportForm.getWarehouseProcessingReportDetailList()){
                    pr.setWarehouseProcessingReportId(warehouseProcessingReport.getId());
                    WarehouseProcessingReportDetail warehouseProcessingReportDetail = new WarehouseProcessingReportDetail();
                    CopyOptions copyOptionsTwo = CopyOptions.create()
                            .setEditable(WarehouseProcessingReportDetail.class)
                            .setIgnoreError(true)
                            .setIgnoreNullValue(true);
                    BeanUtil.copyProperties(pr, warehouseProcessingReportDetail, copyOptionsTwo);
                    warehouseProcessingReportDetailList.add(warehouseProcessingReportDetail);
                    prodictionNumber += pr.getActualQuantity();
                    //添加产品明细
                    WarehouseProductInventory warehouseProductInventory = new WarehouseProductInventory();
                    warehouseProductInventory.setSysBundleCode(UniqueIdGeneratorUtil.generateUniqueId("BC-", "yyyyMMddHHmmss", 1000, 9999));
                    warehouseProductInventory.setUserBundleCode(pr.getFinishedProductBundleCode());
                    warehouseProductInventory.setGrossWeight(pr.getTheoreticalWeight().doubleValue());
                    warehouseProductInventory.setNetWeight(pr.getActualWeight().doubleValue());
                    warehouseProductInventory.setQuantity(pr.getActualQuantity());
                    warehouseProductInventory.setCustomerId(warehouseProcessingReport.getCustomerId());
                    warehouseProductInventory.setWarehouseProductId(warehouseProcessingReport.getProductId());
                    warehouseProductInventory.setOriginalCustomerId(warehouseProcessingReport.getCustomerId());
                    warehouseProductInventory.setSource(pr.getOriginalBundleCode());
                    warehouseProductInventory.setProductStatus(7);
                    warehouseProductInventory.setCustomerId(warehouseProcessingReportForm.getCustomerId());
                    if (ObjectUtil.isNotEmpty(warehouseProductionOrder.getProductVarietyId())){
                        warehouseProductInventory.setWarehouseProductVariety(warehouseProductionOrder.getProductVarietyId().toString());
                    }
                    if (ObjectUtil.isNotEmpty(warehouseProductionOrder.getProductMarkId())){
                        warehouseProductInventory.setWarehouseProductMark(warehouseProductionOrder.getProductMarkId().toString());
                    }
                    if (ObjectUtil.isNotEmpty(warehouseProductionOrder.getProductProducerId())){
                        warehouseProductInventory.setWarehouseProductProducer(warehouseProductionOrder.getProductProducerId().toString());
                    }
                    if (ObjectUtil.isNotEmpty(warehouseProductionSchedulingPlanDetail.getProductSpecCode())){
                        warehouseProductInventory.setSpecifications(warehouseProductionSchedulingPlanDetail.getProductSpecCode());
                    }
                    warehouseProductInventoryList.add(warehouseProductInventory);
                }
                warehouseProductInventoryService.saveBatch(warehouseProductInventoryList);
                warehouseProcessingReportDetailService.saveBatch(warehouseProcessingReportDetailList);
                LambdaUpdateWrapper<WarehouseComputerPractice> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                lambdaUpdateWrapper.eq(WarehouseComputerPractice::getId,warehouseProcessingReportForm.getComputerPracticeId())
                        .set(WarehouseComputerPractice::getStatus,"2");
                warehouseComputerPracticeService.update(lambdaUpdateWrapper);

                //修改生产订单
                if (warehouseProductionOrder.getProcessingReportNumber() != null){
                    warehouseProductionOrder.setProcessingReportNumber(warehouseProductionOrder.getProcessingReportNumber()+prodictionNumber);
                }else {
                    warehouseProductionOrder.setProcessingReportNumber(prodictionNumber);
                }
                warehouseProductionOrderService.updateById(warehouseProductionOrder);

                //修改生产排产计划
                if (warehouseProductionSchedulingPlan.getProcessingReportNumber()!= null){
                    warehouseProductionSchedulingPlan.setProcessingReportNumber(warehouseProductionSchedulingPlan.getProcessingReportNumber()+prodictionNumber);
                }else {
                    warehouseProductionSchedulingPlan.setProcessingReportNumber(prodictionNumber);
                }
                warehouseProductionSchedulingPlanService.updateById(warehouseProductionSchedulingPlan);

                //修改生产排产计划明细
                if (warehouseProductionSchedulingPlanDetail.getProcessingReportNumber()!= null){
                    warehouseProductionSchedulingPlanDetail.setProcessingReportNumber(warehouseProductionSchedulingPlanDetail.getProcessingReportNumber()+prodictionNumber);
                }else {
                    warehouseProductionSchedulingPlanDetail.setProcessingReportNumber(prodictionNumber);
                }
                warehouseProductionSchedulingPlanDetailService.updateById(warehouseProductionSchedulingPlanDetail);

            }
            return JsonResult.success("添加成功");
        }else {
            return JsonResult.fail("添加失败");
        }
    }

    @Override
    @Transactional
    public JsonResult updateProcessingReport(WarehouseProcessingReportForm warehouseProcessingReportForm){

        if (ObjectUtil.isNotEmpty(warehouseProcessingReportForm)){
            WarehouseProcessingReport warehouseProcessingReport = new WarehouseProcessingReport();
            CopyOptions copyOptions = CopyOptions.create()
                    .setEditable(WarehouseProcessingReport.class)
                    .setIgnoreError(true)
                    .setIgnoreNullValue(true);
            BeanUtil.copyProperties(warehouseProcessingReportForm, warehouseProcessingReport, copyOptions);
            warehouseProcessingReport.setUpdateTime(LocalDateTime.now());
            this.baseMapper.updateById(warehouseProcessingReport);
            LambdaQueryWrapper<WarehouseProcessingReportDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(WarehouseProcessingReportDetail::getWarehouseProcessingReportId,warehouseProcessingReport.getId())
                    .eq(WarehouseProcessingReportDetail::getIsDeleted,0);
            warehouseProcessingReportDetailDao.delete(lambdaQueryWrapper);
            if (CollectionUtils.isNotEmpty(warehouseProcessingReportForm.getWarehouseProcessingReportDetailList())){
                List<WarehouseProcessingReportDetail> warehouseProcessingReportDetailList = new ArrayList<>();
                for (WarehouseProcessingReportDetailForm pr : warehouseProcessingReportForm.getWarehouseProcessingReportDetailList()){
                    pr.setWarehouseProcessingReportId(warehouseProcessingReport.getId());
                    WarehouseProcessingReportDetail warehouseProcessingReportDetail = new WarehouseProcessingReportDetail();
                    CopyOptions copyOptionsTwo = CopyOptions.create()
                            .setEditable(WarehouseProcessingReportDetail.class)
                            .setIgnoreError(true)
                            .setIgnoreNullValue(true);
                    BeanUtil.copyProperties(pr, warehouseProcessingReportDetail, copyOptionsTwo);
                    warehouseProcessingReportDetail.setUpdateTime(LocalDateTime.now());
                    warehouseProcessingReportDetailList.add(warehouseProcessingReportDetail);
                }
                warehouseProcessingReportDetailService.saveBatch(warehouseProcessingReportDetailList);
            }
            return JsonResult.success("修改成功");
        }else {
            return JsonResult.fail("修改失败");
        }
    }
}
